package com.muyu.vehicle.controller;

import java.net.URLEncoder;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.muyu.vehicle.config.Config;
import com.muyu.vehicle.entity.MsgDecoder;
import com.muyu.vehicle.entity.PackageData;
import com.muyu.vehicle.entity.request.LocationMsg;
import com.muyu.vehicle.utils.HttpClientUtil;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.vehicle.common.log.annotation.Log;
import com.vehicle.common.log.enums.BusinessType;
import com.vehicle.common.security.annotation.PreAuthorize;
import com.muyu.vehicle.domain.VehicleMoveInfo;
import com.muyu.vehicle.service.IVehicleMoveInfoService;
import com.vehicle.common.core.web.controller.BaseController;
import com.vehicle.common.core.web.domain.AjaxResult;
import com.vehicle.common.core.utils.poi.ExcelUtil;
import com.vehicle.common.core.web.page.TableDataInfo;
import org.springframework.web.client.RestTemplate;

/**
 * 车辆运行记录Controller
 *
 * @author muyu
 * @date 2021-09-04
 */
@RestController
@RequestMapping("/move")
public class VehicleMoveInfoController extends BaseController
{
    @Autowired
    private IVehicleMoveInfoService vehicleMoveInfoService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 查询车辆运行记录列表
     */
    @PreAuthorize(hasPermi = "vehicle:move:list")
    @GetMapping("/list")
    public TableDataInfo list(VehicleMoveInfo vehicleMoveInfo)
    {
        startPage();
        List<VehicleMoveInfo> list = vehicleMoveInfoService.selectVehicleMoveInfoList(vehicleMoveInfo);
        return getDataTable(list);
    }

    /**
     * 导出车辆运行记录列表
     */
    @PreAuthorize(hasPermi = "vehicle:move:export")
    @Log(title = "车辆运行记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, VehicleMoveInfo vehicleMoveInfo) throws IOException
    {
        List<VehicleMoveInfo> list = vehicleMoveInfoService.selectVehicleMoveInfoList(vehicleMoveInfo);
        ExcelUtil<VehicleMoveInfo> util = new ExcelUtil<VehicleMoveInfo>(VehicleMoveInfo.class);
        util.exportExcel(response, list, "车辆运行记录数据");
    }

    /**
     * 获取车辆运行记录详细信息
     */
    @PreAuthorize(hasPermi = "vehicle:move:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(vehicleMoveInfoService.selectVehicleMoveInfoById(id));
    }

    /**
     * 新增车辆运行记录
     */
    @PreAuthorize(hasPermi = "vehicle:move:add")
    @Log(title = "车辆运行记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody VehicleMoveInfo vehicleMoveInfo)
    {
        return toAjax(vehicleMoveInfoService.addVehicleMoveInfo(vehicleMoveInfo));
    }

    /**
     * 修改车辆运行记录
     */
    @PreAuthorize(hasPermi = "vehicle:move:edit")
    @Log(title = "车辆运行记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody VehicleMoveInfo vehicleMoveInfo)
    {
        return toAjax(vehicleMoveInfoService.updateVehicleMoveInfo(vehicleMoveInfo));
    }

    /**
     * 删除车辆运行记录
     */
    @PreAuthorize(hasPermi = "vehicle:move:remove")
    @Log(title = "车辆运行记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(vehicleMoveInfoService.deleteVehicleMoveInfoByIds(ids));
    }


    /**
     * 车辆实时数据
     */
    @GetMapping("/historyMove/{vin}/{startTime}/{endTime}")
    public AjaxResult historyMoveStart(
            @PathVariable(value = "vin") String vin,
            @PathVariable(value = "startTime") String startTime,
            @PathVariable(value = "endTime") String endTime
    )
    {
        HttpClientUtil.doGet(
                "http://127.0.0.1:10002/test?vin="+vin+
                        "&startTime="+ URLEncoder.encode(startTime) +"&endTime="+ URLEncoder.encode(endTime));
        return AjaxResult.success();
    }


    /**
     * 车辆实时数据
     */
    @DeleteMapping("/historyMove/{vin}")
    public AjaxResult historyMoveDel(@PathVariable(value = "vin") String vin)
    {
        redisTemplate.delete(Config.VEHICLE_HISTORY_LOCUS + vin);
        return AjaxResult.success();
    }


    /**
     * 车辆实时数据
     */
    @PreAuthorize(hasPermi = "vehicle:info:remove")
    @GetMapping("/history/{vin}")
    public AjaxResult vehicleDataByVin(@PathVariable(value = "vin") String vin)
    {
        String vehicleData = redisTemplate.opsForList().rightPop(Config.VEHICLE_HISTORY_LOCUS + vin);
        return AjaxResult.success(JSONObject.parseObject(vehicleData));
    }




    private MsgDecoder decoder = new MsgDecoder();

    @GetMapping("/selectRedis")
    public AjaxResult selectRedis(){
        PackageData jt808Msg = new PackageData();
        LocationMsg msg = this.decoder.toLocationMsg(jt808Msg);
        String terminalMsg = stringRedisTemplate.opsForValue().get("terminal:" + msg.getLocationInfo());
        return AjaxResult.success(terminalMsg);
    }





}
